// User and Authentication Types
export interface User {
  id: number
  username: string
  email: string
  name: string
  avatar?: string
  role: string
  permissions: string[]
  created_at: string
  updated_at: string
}

export interface LoginRequest {
  username: string
  password: string
}

export interface LoginResponse {
  success: boolean
  message: string
  data: {
    token: string
    user: User
    permissions: string[]
  }
}

export interface GetCurrentUserResponse {
  success: boolean
  message: string
  data: {
    user: User
    permissions: string[]
  }
}

// Chat and AI Types
export interface SQLStatement {
  content: string
  type: string
  info: string
  riskLevel: string
}

export interface ChatMessage {
  id?: number
  role: 'user' | 'assistant' | 'system'
  content: string
  timestamp: Date
  session_id?: string
  metadata?: Record<string, any>
  messageType?: 'text' | 'table' | 'action' | 'chart' | 'sql'
  data?: any // 结构化数据 (表格数据、图表数据等)
  sqlStatements?: SQLStatement[] // SQL语句数组
}

export interface ChatSession {
  id: string
  title: string
  created_at: Date
  updated_at: Date
  message_count: number
  token_usage: number
}

export interface ChatHistoryResponse {
  success: boolean
  data: ChatMessage[]
  message: string
}

export interface AIResponse {
  success: boolean
  message: string
  data: {
    message: string
    context: ChatMessage[]
    usage?: {
      total_tokens: number
      prompt_tokens?: number
      completion_tokens?: number
    }
  }
}

export interface AIQueryRequest {
  message: string
  context?: ChatMessage[]
  session_id?: string
}

// Theme Types
export type ThemeMode = 'light' | 'dark' | 'auto'

export interface ThemeConfig {
  mode: ThemeMode
  primaryColor: string
  accentColor: string
  customColors?: Record<string, string>
}

// Component Props Types
export interface BaseComponentProps {
  class?: string
  id?: string
}

export interface ButtonProps extends BaseComponentProps {
  variant?: 'primary' | 'secondary' | 'ghost' | 'outline' | 'danger'
  size?: 'sm' | 'md' | 'lg'
  loading?: boolean
  disabled?: boolean
  icon?: string
  iconPosition?: 'left' | 'right'
  type?: 'button' | 'submit' | 'reset'
}

export interface InputProps extends BaseComponentProps {
  modelValue?: string | number
  type?: 'text' | 'email' | 'password' | 'number' | 'tel' | 'url'
  placeholder?: string
  disabled?: boolean
  readonly?: boolean
  required?: boolean
  error?: string
  label?: string
  hint?: string
}

export interface MessageProps extends BaseComponentProps {
  message: ChatMessage
  showTimestamp?: boolean
  showAvatar?: boolean
  compact?: boolean
}

// Business Data Types (from API analysis)
export interface Product {
  id: number
  name: string
  sku: string
  description?: string
  category_id?: number
  price: number
  cost?: number
  status: 'active' | 'inactive' | 'discontinued'
  created_at: string
  updated_at: string
  inventory?: Inventory
}

export interface Customer {
  id: number
  name: string
  email?: string
  phone?: string
  address?: string
  company?: string
  status: 'active' | 'inactive'
  created_at: string
  updated_at: string
}

export interface Order {
  id: number
  customer_id: number
  order_number: string
  status: 'pending' | 'confirmed' | 'shipped' | 'delivered' | 'cancelled'
  total_amount: number
  created_at: string
  updated_at: string
  customer?: Customer
  items?: OrderItem[]
}

export interface OrderItem {
  id: number
  order_id: number
  product_id: number
  quantity: number
  unit_price: number
  total_price: number
  product?: Product
}

export interface Inventory {
  id: number
  product_id: number
  quantity: number
  reserved_quantity: number
  min_stock: number
  max_stock?: number
  updated_at: string
  product?: Product
}

// API Response Types
export interface APIResponse<T = any> {
  success: boolean
  message: string
  data?: T
  error?: string
  code?: number
}

export interface PaginatedResponse<T> {
  success: boolean
  data: T[]
  pagination: {
    current_page: number
    per_page: number
    total: number
    total_pages: number
    has_next: boolean
    has_prev: boolean
  }
  message: string
}

// Business Analytics Types
export interface BusinessStats {
  totalProducts: number
  totalCustomers: number
  totalOrders: number
  pendingOrders: number
  lowStockCount: number
  totalRevenue: number
}

export interface SalesData {
  date: string
  orderCount: number
  totalAmount: number
}

export interface AnalyticsResponse {
  code: number
  message: string
  data: {
    sales?: SalesData[]
    lowStock?: (Inventory & { product: Product })[]
    topCustomers?: (Customer & { orderCount: number; totalSpent: number })[]
    stats?: BusinessStats
  }
}

// Dynamic Menu Types
export interface DynamicMenu {
  id: number
  name: string
  key: string
  icon?: string
  table_name: string
  description?: string
  permissions?: string[]
  columns_config?: any
  sort_order: number
  status: 'active' | 'inactive'
  created_by?: number
  created_at: string
  updated_at: string
  // 关联数据
  creator?: {
    id: number
    username: string
    realName: string
  }
}

// SQL Log Types
export interface SQLLog {
  id: number
  user_id: number
  session_id?: string
  user_query: string
  ai_response?: string
  sql_statement?: string
  sql_type?: 'SELECT' | 'INSERT' | 'UPDATE' | 'DELETE' | 'CREATE' | 'ALTER' | 'DROP' | 'OTHER'
  target_table?: string
  execution_status: 'pending' | 'success' | 'failed' | 'cancelled'
  execution_time?: number
  rows_affected?: number
  error_message?: string
  execution_result?: any
  risk_level: 'low' | 'medium' | 'high' | 'critical'
  is_approved: boolean
  approved_by?: number
  approved_at?: string
  ip_address?: string
  user_agent?: string
  ai_model: string
  token_usage: number
  created_at: string
  updated_at: string
  executed_at?: string
  // 关联数据
  user?: {
    id: number
    username: string
    realName: string
    email: string
  }
  approver?: {
    id: number
    username: string
    realName: string
  }
}

// Data Visualization Types
export interface ChartData {
  labels: string[]
  datasets: {
    label: string
    data: number[]
    backgroundColor?: string | string[]
    borderColor?: string | string[]
    borderWidth?: number
  }[]
}

export interface ChartOptions {
  responsive: boolean
  maintainAspectRatio: boolean
  plugins?: {
    legend?: {
      display: boolean
      position?: 'top' | 'bottom' | 'left' | 'right'
    }
    title?: {
      display: boolean
      text: string
    }
  }
  scales?: {
    x?: {
      display: boolean
      title?: {
        display: boolean
        text: string
      }
    }
    y?: {
      display: boolean
      title?: {
        display: boolean
        text: string
      }
    }
  }
}

// Token Usage Types
export interface TokenUsage {
  id: number
  user_id: number
  session_id: string
  input_tokens: number
  output_tokens: number
  total_tokens: number
  cost: number
  model: string
  created_at: string
}

export interface TokenStats {
  totalTokens: number
  totalCost: number
  sessionsCount: number
  averageTokensPerSession: number
  dailyUsage: {
    date: string
    tokens: number
    cost: number
    messages: number
  }[]
}

// Error Types
export interface AppError {
  code: string
  message: string
  details?: string
  timestamp: Date
}

// Route Types
export interface RouteConfig {
  path: string
  name: string
  component: any
  meta?: {
    requiresAuth?: boolean
    permissions?: string[]
    title?: string
    icon?: string
    hideInMenu?: boolean
  }
}

// Store Types
export interface RootState {
  auth: AuthState
  chat: ChatState
  theme: ThemeState
  app: AppState
}

export interface AuthState {
  user: User | null
  token: string | null
  isAuthenticated: boolean
  permissions: string[]
  loading: boolean
}

export interface ChatState {
  sessions: ChatSession[]
  currentSession: ChatSession | null
  messages: ChatMessage[]
  loading: boolean
  typing: boolean
  context: ChatMessage[]
}

export interface ThemeState {
  mode: ThemeMode
  config: ThemeConfig
  systemTheme: 'light' | 'dark'
}

export interface AppState {
  loading: boolean
  error: AppError | null
  notifications: Notification[]
  sidebar: {
    collapsed: boolean
    mobileOpen: boolean
  }
}

// Notification Types
export interface Notification {
  id: string
  type: 'success' | 'error' | 'warning' | 'info'
  title: string
  message?: string
  duration?: number
  persistent?: boolean
  read?: boolean
  timestamp?: Date
  action?: {
    label: string
    handler: () => void
  }
}
